********************************************************************************
** 	TITLE:		ON99_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ON99_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* 1. Download the "Valid Votes Cast for Each Candidate" file from this link: https://results.elections.on.ca/api/report-groups/6/report-outputs/532/csv  
* 2. Save the file to the CSV format with the following name: "ON99_results.csv"

* Import the dataset

import delimited "ON99_results.csv"

* Keep only general election

drop if isgeneralelection == 0

* Rename and (re)create variables

rename electoraldistrictnumber districtnumber99
rename electoraldistrictnameenglish districtname99
rename politicalinterestcode partyname99
rename totalvalidballotscast numberofvotes99
gen numberofvotespercent99 = percentoftotalvalidballotscast*100

* Generate dichotomous variable for each party

gen lib99 = 1 if partyname99 == "L"
replace lib99 = 0 if lib99!=1

gen con99 = 1 if partyname99 == "PC"
replace con99 = 0 if con99!=1

gen ndp99 = 1 if partyname99 == "ND"
replace ndp99 = 0 if ndp99!=1

gen green99 = 1 if partyname99 == "GP"
replace green99 = 0 if green99!=1

* Generate party variable

gen party99 = 1 if lib99 == 1
replace party99 = 2 if con99 == 1
replace party99 = 3 if ndp99 == 1
replace party99 = 4 if green99 == 1
replace party99 = 88 if party99 == . 

* Generate rank of parties

gsort districtname99 -numberofvotes99
bysort districtname99 : gen rank99 = _n

gen first99 = party99 if rank99 == 1
gen second99 = party99 if rank99 == 2
gen third99 = party99 if rank99 == 3

bysort districtname99 : gen winner99 = first99[1]
bysort districtname99 : gen runnerup99 = second99[2]
bysort districtname99 : gen thirdplace99 = third99[3]

* Generate margins of victory

bysort districtname99 : gen margin99 = numberofvotespercent99[1] - numberofvotespercent99[2]

* Generate effective number of electoral parties

gen sqrvotepr99 = (numberofvotespercent99/100)^2

bysort districtname99 : egen sumsqrvotepr99 = sum(sqrvotepr99)

gen enep99 = 1/sumsqrvotepr99

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "New Democratic Party" 4 "Green Party" 88 "Other"
label values winner99 runnerup99 thirdplace99 party

* Selecting, ordering, and sorting variables

keep districtnumber99 districtname99 party99 rank99 numberofvotes99 numberofvotespercent99 margin99 enep99 winner99 runnerup99 thirdplace99

order districtnumber99 districtname99 party99 rank99 numberofvotes99 numberofvotespercent99 margin99 enep99 winner99 runnerup99 thirdplace99

sort districtnumber99 rank99

drop if rank99 > 3

* Drop duplicates

duplicates tag districtnumber99, gen(dup)
duplicates drop districtnumber99, force
drop dup

* Create election variable

gen election99 = "1999 Ontario general election"

* Saving file

save ON99_results, replace